<template>
    <view class="forgot-container">
       <!-- 使用封装的页面头部组件 -->
       <page-header title="修改密码"></page-header>

        <view class="title">修改密码</view>
        <view class="subtitle">请输入以下信息修改密码</view>

        <view class="form-box">
            <view class="input-item">
                <input
                    :type="showOldPassword ? 'text' : 'password'"
                    v-model="oldPassword"
                    placeholder="请输入原密码"
                    placeholder-class="placeholder"
                />
                <view class="eye-icon" @click="toggleOldPasswordVisibility">
                    <roc-icon-plus
                        type="fas"
                        :name="showOldPassword ? 'eye' : 'eye-slash'"
                        :size="20"
                        color="#999"
                    ></roc-icon-plus>
                </view>
            </view>

            <view class="input-item">
                <input
                    :type="showNewPassword ? 'text' : 'password'"
                    v-model="newPassword"
                    placeholder="请输入新密码"
                    placeholder-class="placeholder"
                />
                <view class="eye-icon" @click="toggleNewPasswordVisibility">
                    <roc-icon-plus
                        type="fas"
                        :name="showNewPassword ? 'eye' : 'eye-slash'"
                        :size="20"
                        color="#999"
                    ></roc-icon-plus>
                </view>
            </view>

            <view class="input-item">
                <input
                    :type="showConfirmPassword ? 'text' : 'password'"
                    v-model="confirmPassword"
                    placeholder="请确认新密码"
                    placeholder-class="placeholder"
                />
                <view class="eye-icon" @click="toggleConfirmPasswordVisibility">
                    <roc-icon-plus
                        type="fas"
                        :name="showConfirmPassword ? 'eye' : 'eye-slash'"
                        :size="20"
                        color="#999"
                    ></roc-icon-plus>
                </view>
            </view>

            <button class="confirm-btn" @click="handleConfirm">确认修改</button>
        </view>
    </view>
</template>

<script>
import { user } from '@/utils/api/index.js';
import PageHeader from "@/components/PageHeader/index.vue";
export default {
    components: {
        PageHeader
    },
    data() {
        return {
            oldPassword: "",
            newPassword: "",
            confirmPassword: "",
            showOldPassword: false,
            showNewPassword: false,
            showConfirmPassword: false,
        };
    },
    methods: {
        goBack() {
            uni.navigateBack();
        },
        toggleOldPasswordVisibility() {
            this.showOldPassword = !this.showOldPassword;
        },
        toggleNewPasswordVisibility() {
            this.showNewPassword = !this.showNewPassword;
        },
        toggleConfirmPasswordVisibility() {
            this.showConfirmPassword = !this.showConfirmPassword;
        },
        handleConfirm() {
            if (!this.oldPassword) {
                uni.showToast({
                    title: "请输入原密码",
                    icon: "none",
                });
                return;
            }

            if (
                !this.newPassword ||
                this.newPassword.length < 6 ||
                this.newPassword.length > 20
            ) {
                uni.showToast({
                    title: "请输入6-20位新密码",
                    icon: "none",
                });
                return;
            }

            if (this.newPassword !== this.confirmPassword) {
                uni.showToast({
                    title: "两次密码输入不一致",
                    icon: "none",
                });
                return;
            }

            // 调用修改密码接口
            uni.showLoading({
                title: "修改中...",
            });

            const data = {
                oldPassword: this.oldPassword,
                newPassword: this.newPassword
            };

            user.updatePassword(data).then(res => {
                uni.hideLoading();
                
                if (res.code === 0) {
                    uni.showToast({
                        title: "密码修改成功",
                        icon: "success",
                        success: () => {
                            setTimeout(() => {
                                uni.navigateBack();
                            }, 1500);
                        }
                    });
                } else {
                    uni.showToast({
                        title: res.msg || "密码修改失败",
                        icon: "none",
                    });
                }
            }).catch(err => {
                uni.hideLoading();
                uni.showToast({
                    title: err.msg || "密码修改失败，请稍后再试",
                    icon: "none",
                });
                console.error('修改密码失败:', err);
            });
        },
    },
};
</script>

<style lang="scss">
.forgot-container {
    min-height: 100vh;
    padding: 150rpx 60rpx 0; 
    background-color: #fff;

    .header {
        position: absolute;
        left: 20rpx;
        top: 80rpx;
        height: 88rpx;
        display: flex;
        align-items: center;
        margin-top: 20rpx;

        .back-icon {
            padding: 10rpx;
        }
    }

    .title {
        font-size: 40rpx;
        font-weight: 600;
        color: #333;
        margin-top: 20rpx;
    }

    .subtitle {
        font-size: 28rpx;
        color: #999;
        margin-top: 10rpx;
        margin-bottom: 60rpx;
    }

    .form-box {
        .input-item {
            display: flex;
            align-items: center;
            height: 100rpx;
            border-bottom: 1px solid #eeeeee;
            margin-bottom: 30rpx;

            input {
                flex: 1;
                height: 100%;
                font-size: 30rpx;
            }

            .placeholder {
                color: #999;
            }

            .eye-icon {
                padding: 0 10rpx;
            }
        }

        .confirm-btn {
            height: 90rpx;
            line-height: 90rpx;
            text-align: center;
            background-color: #2eb57d;
            color: #fff;
            font-size: 32rpx;
            border-radius: 12rpx;
            margin-top: 60rpx;
            margin-bottom: 40rpx;
        }
    }
}
</style> 